import { config } from "../config.js";
import { MyModel } from "../../../modules/my.js";
import { HomeModel } from '../../../modules/home.js';
import { config as rootConfig } from '../../../config.js';
import { config as _rootConfig } from '../../../project.config.js'; 

const app = getApp();
const my = new MyModel();
const home = new HomeModel();

Page({
  data: {
    recruitingHeight: app.globalData.screenHeight - 368,
    positions: [],  // 储存所有职位
    comp: null, // 企业信息
    enterprise: {   // 默认数据
      name: config.enterprise.name,
      scale: config.enterprise.scale,
      field: config.enterprise.field,
      text: config.enterprise.text
    },
    PATTERN: '',
    loading: false
  },
  // 在招职位
  requestData() {
    home.requestData()
    .then(res => {
      this.setData({
        positions: res.data.list,
        loading: true
      });
      wx.stopPullDownRefresh();
    })
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: rootConfig.TITLE
    })
    let compId = options.compId;
    if(compId) {
      rootConfig.COMP_ID = compId;
      _rootConfig.PATTERN = 'comp';
      this.setData({
        PATTERN: 'comp',
      })
      wx.hideTabBar({
        animation: true
      })
    }
    // 企业信息
    home.compInfo()
    .then(res => {
      this.setData({
        comp: res.data.comp
      })
    })
    app.isAuthize()
    .then(res => {
      if(res) {
        // 获取用户简历到缓存
        my.setResume()
      }
    });
    this.requestData();
  },
  // 下拉刷新
  lower: function() {
    wx.showNavigationBarLoading();
    home.downRequestData()
    .then(res => {
      this.setData({
        positions: this.data.positions.concat(res.data.list)
      });
      setTimeout(function () {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }, 1000)
    })
  },
  // 上拉刷新
  onPullDownRefresh: function () {
    this.requestData();
  },
  //  用户点击右上角分享
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      console.log(ops.target)
    }
    return {
      title: rootConfig.SHARE_TITLE,
      path: '/pages/home/index/index',
      success: res => {
        console.log("转发成功:", JSON.stringify(res));
      },
      fail: err => {
        console.log("转发失败:", JSON.stringify(err));
      }
    }
  }
})